<template>
    <ul :class="ulName">
        <li
            @click="toDetail(item.proid)"
            v-for="item in list"
            :key="item.proid"
            class="item"
        >
            <!-- 商品图 -->
            <div class="itemImage">
                <van-image :src="item.img1" />
            </div>
            <!-- 商品描述信息 -->
            <div class="itemInfo">
                <!-- 借助 vant 提供的类名, 让我们的文本超过两行自动隐藏 -->
                <div class="van-multi-ellipsis--l2">{{ item.proname }}</div>
                <div class="itemInfoPrice">{{ item.originprice }}</div>
            </div>
        </li>
    </ul>
    <div class="icon_box">
        <!-- <van-icon name="close" size="40" /> -->
        <!-- <van-icon name="add-o" size="40" /> -->
        <van-icon
            :name="iconName"
            size="40"
            @click="iconName = iconName === 'add-o' ? 'close' : 'add-o'"
        />
    </div>
</template>

<script setup lang="ts">
import { defineProps, ref, computed } from "vue";
import type { Ref } from "vue";
import { useRouter } from "vue-router";
defineProps(["list"]);

// 创建一个 以前的 this.$router
const router = useRouter();

// 存储 图标的 名字
const iconName: Ref<string> = ref("add-o");

// 根据 图标 决定 当前 ul 的类名, 然后控制商品的排列
const ulName = computed(() => {
    return iconName.value === "add-o" ? "clo_1" : "clo_2";
});

// 跳转商品详情
function toDetail(id: string) {
    // console.log('跳转')

    // router.push('/detail')

    router.push({
        // path: '/detail'
        name: "detail",
        query: {
            id,
        },
    });
}
</script>

<style scoped lang="scss">
// .clo_1 左图右文
.clo_1 {
    padding: 10px;
    box-sizing: border-box;
    .item {
        display: flex;
        border: 1px solid #f66;
        margin: 3px 0;

        .itemImage {
            width: 1rem;
            height: 1rem;
            .van-image {
                width: 100%;
                height: 100%;
            }
        }
        .itemInfo {
            flex: 1;
            display: flex;
            flex-direction: column;
            justify-content: space-evenly;
            padding: 0 10px;
            box-sizing: border-box;

            .itemInfoPrice {
                font-size: 20px;
                color: #f66;
            }
        }
    }
}

// .clo_2 上图下文
.clo_2 {
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    box-sizing: border-box;
    .item {
        width: 48%;
        height: 251px;
        border: 1px solid #f66;
        box-sizing: border-box;
        margin: 5px 3px;
        .itemImage {
            width: 100%;
            height: 1.7rem;
            .van-image {
                width: 100%;
                height: 100%;
            }
        }
        .itemInfo {
            padding: 8px;
            display: flex;
            flex-direction: column;
            align-items: flex-end;
            .itemInfoPrice {
                font-size: 20px;
                color: #f66;
            }
        }
    }
}

.icon_box {
    position: fixed;
    right: 30px;
    bottom: 80px;
}
</style>
